<template>
  <ul class="anchor">
    <li v-for="item in menus" :id="item.id" :key="item.id">
      <el-tooltip class="item" effect="light" :content="item.title" placement="right-end">
        <h1 v-if="item.h === 1"><a :href="`#${item.id}`" @click="clickItem(item.id)">{{ item.title }}</a></h1>
        <h2 v-if="item.h === 2"><a :href="`#${item.id}`" @click="clickItem(item.id)">{{ item.title }}</a></h2>
        <h3 v-if="item.h === 3"><a :href="`#${item.id}`" @click="clickItem(item.id)">{{ item.title }}</a></h3>
        <h4 v-if="item.h === 4"><a :href="`#${item.id}`" @click="clickItem(item.id)">{{ item.title }}</a></h4>
        <h5 v-if="item.h === 5"><a :href="`#${item.id}`" @click="clickItem(item.id)">{{ item.title }}</a></h5>
        <h6 v-if="item.h === 6"><a :href="`#${item.id}`" @click="clickItem(item.id)">{{ item.title }}</a></h6>
      </el-tooltip>
      <ul v-if="item[childLabel]">
        <menu-tree :menus="item[childLabel]" :child-label="childLabel" />
      </ul>
    </li>
  </ul>
</template>

<script>
export default {
  name: 'MenuTree',
  props: {
    menus: {
      type: [Object, Array],
      required: true
    },
    childLabel: {
      type: String,
      default: 'child'
    }
  },
  data() {
    return {
    }
  },
  methods: {
    clickItem(id) {
      const menus = document.querySelector('.anchor').querySelectorAll('li')
      for (let i = 0; i < menus.length; i++) {
        menus[i].classList.remove('active')
        if (menus[i].getAttribute('id') === id) {
          menus[i].classList.add('active')
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.anchor {
  overflow-y: auto;
  flex: 1;
  max-width: 250px;
  font-size: 10px;
  padding-inline-start: 0px;

  ul {
    list-style: none;
    padding: 0;
    margin: 0;
    padding-inline-start: 0px;
  }

  li {
    list-style: none;
    padding: 0;
    margin: 0;
    padding-inline-start: 0px;
  }

  h1, h2, h3, h4, h5, h6 {
    margin: 2px 0;
    font-weight: 500;
    font-size: 17px;
    color: #2185d0;
    cursor: pointer;
    line-height: normal;
    overflow: hidden;
    white-space: nowrap;
    border-bottom: none;
    text-overflow: ellipsis;
    &:hover {
      color: #483D8B;
      text-decoration-line: underline;
    }
  }
  h2 {
    padding-left: 10px;
    font-size: 17px;
  }
  h3 {
    padding-left: 20px;
    font-size: 17px;
  }
  h4 {
    padding-left: 30px;
    font-size: 15px;
  }
  h5 {
    padding-left: 40px;
    font-size: 15px;
  }
  h6 {
    padding-left: 50px;
    font-size: 15px;
  }
}
</style>
